//
//  Loading.m
//  MountEverestFM
//
//  Created by ChenNing on 15/11/4.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "Loading.h"

@implementation Loading

// 初始化转圈的imageView
- (instancetype)showLoadingView
{
    _loading = [[Loading alloc] init];
    _loading.loadingImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, FM_Screen_Pts_Width*50, FM_Screen_Pts_Width*50)];
    _loading.loadingImageView.image = [UIImage imageNamed:@"tabbar_np_loop@2x.png"];
    _loading.loadingImageView.center = self.center;
    [self addSubview:_loading];
    [_loading startLoading];
    return _loading;
}

// 转圈动画
- (void) spinWithOptions: (UIViewAnimationOptions) options {
    [UIView animateWithDuration: 0.2f
                          delay: 0.0f
                        options: options
                     animations: ^{
                         _loadingImageView.transform = CGAffineTransformRotate(_loadingImageView.transform, M_PI / 2);
                     }
                     completion: ^(BOOL finished) {
                         if (finished) {
                             if (_isRun) {
                                 [self spinWithOptions: UIViewAnimationOptionCurveLinear];
                             } else if (options != UIViewAnimationOptionCurveEaseOut) {
                                 [self spinWithOptions: UIViewAnimationOptionCurveEaseOut];
                             }
                         }
                     }];
}

// 开始转圈
- (void) startLoading {
    if (!_isRun) {
        _isRun = !_isRun;
        [self.loadingImageView removeFromSuperview];
        [self spinWithOptions: UIViewAnimationOptionCurveEaseIn];
    }
}

// 停止转圈
- (void) stopLoading {
    [self.loadingImageView removeFromSuperview];
    _isRun = NO;
}

- (void)openActivity

{
    
    // 添加网络指示器
    
    _activityIV = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    
    [_activityIV setFrame:CGRectMake(0, 0, 30, 30)];
    
    [_activityIV setCenter:CGPointMake(self.center.x, self.center.y)];
    
    [self addSubview:_activityIV];
    
    [_activityIV setHidesWhenStopped:YES];
    
    [_activityIV setHidden:NO];
    
    [_activityIV startAnimating];
    
    
    
    // 在状态栏显示有网络请求的提示器
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
}



- (void)closeActivity

{
    
    // 关闭网络指示器
    
    [_activityIV setHidden:YES];
    
    [_activityIV stopAnimating];
    
    
    
    // 在状态栏关闭有网络请求的提示器
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    
}


@end
